﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Library;
using System.Data;

/*
-- =============================================
-- Author:		Duong Hoang Vinh
-- Create date: 3/11/2011
-- Description:	NewsType Class
-- =============================================
*/

namespace BusinessLayer
{
   public class NewsType
    {
    #region "Variables and Properties"
    //Variables
        private int news_Type_ID;        
        private string news_Type_Name;
        private string news_Type_Description;        
        private int news_Type_Status;        

    //Properties
        public int News_Type_ID
        {
            get { return news_Type_ID; }
            set { news_Type_ID = value; }
        }

        public string News_Type_Name
        {
            get { return news_Type_Name; }
            set { news_Type_Name = value; }
        }

        public string News_Type_Description
        {
            get { return news_Type_Description; }
            set { news_Type_Description = value; }
        }

        public int News_Type_Status
        {
            get { return news_Type_Status; }
            set { news_Type_Status = value; }
        }
    #endregion

    #region "Initial Methods"
        public NewsType() { }

        public NewsType(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                News_Type_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    News_Type_Name = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    News_Type_Description = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    News_Type_Status = sdr.GetInt32(3);
            }
        }
    #endregion

    #region "Business Methods"
    //Get All News Type
        public static List<NewsType> GetAll()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_Type_SHOWALL";
            List<NewsType> lstNewsType = new List<NewsType>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            while (sdr.Read())
            {
                lstNewsType.Add(new NewsType(sdr));
            }

            return lstNewsType;
        }

    //Get One News Type
        public static NewsType GetOne(int idNewsType)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_Type_SHOWONE";
            NewsType newsType = new NewsType();
            SqlDataReader sdr;
            
            dbh.AddParameter("@News_Type_ID", idNewsType);
            sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            if (sdr.Read())
            {
                newsType.News_Type_ID = sdr.GetInt32(0);
                if(!sdr.IsDBNull(1))
                newsType.News_Type_Name = sdr.GetString(1);
                if(!sdr.IsDBNull(2))
                newsType.News_Type_Description = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    newsType.News_Type_Status = sdr.GetInt32(3);
            }

            return newsType;
        }

    //Insert News Type
        public static void Insert(NewsType newsType)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_Type_INSERT";
            dbh.AddParameter("@News_Type_Name", newsType.News_Type_Name);
            dbh.AddParameter("@News_Type_Description", newsType.News_Type_Description);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Update News Type
        public static void Update(NewsType newsType)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_Type_UPDATE";
            dbh.AddParameter("@News_Type_ID", newsType.News_Type_ID);
            dbh.AddParameter("@News_Type_Name", newsType.News_Type_Name);
            dbh.AddParameter("@News_Type_Description", newsType.News_Type_Description);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Delete News Type
        public static void Delete(int idNewsType)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_Type_DELETE";

            dbh.AddParameter("@News_Type_ID", idNewsType);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    #endregion
    }
}
